package com.example.demo1.controller;


import com.example.demo1.dao.EmpMapper;
import com.example.demo1.enums.EmpInfoEnum;
import com.example.demo1.pojo.po.Emp;
import com.example.demo1.pojo.vo.EmpVO;
import com.example.demo1.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Description: 本类用于CRUD员工信息
 * Created by DWB on 2019/12/19 14:38
 */

@RestController
@Slf4j
public class EmpController {

    @Autowired
    private EmpMapper empMapper;

    /**
     * 根据id查询员工信息
     *
     * @param id 参数id
     * @return 返回员工信息
     */
    @GetMapping("/getEmpById")
    public ResultUtil getEmpById(@RequestParam("id") Integer id) {
        if (id == null) {
            return ResultUtil.failed(EmpInfoEnum.idIsNull.getStatusCode(),EmpInfoEnum.idIsNull.getStatusMessage());
        }
            try {
                Emp emp = empMapper.getEmpById(id);
                EmpVO empVO=new EmpVO();
                BeanUtils.copyProperties(emp,empVO);
               if(emp!=null){
                   return ResultUtil.success(empVO);
               }else{
                   return ResultUtil.success(EmpInfoEnum.resultNotFound.getStatusMessage());
               }
            } catch (Exception e) {
                e.printStackTrace();
                return ResultUtil.failed(EmpInfoEnum.queryExceptionById.getStatusCode(),EmpInfoEnum.queryExceptionById.getStatusMessage());
            }
        }

        @GetMapping("/selectAll")
        public ResultUtil selectAllEmp(){
            List<Emp> empList=empMapper.selectAllByEmp();
            log.info("==++++++=="+empList);
            return ResultUtil.success(empList);
        }


    private void setValue(Emp emp, EmpVO empVO) {
        empVO.setEmpAge(emp.getEmpAge());
        empVO.setEmpName(emp.getEmpName());
        empVO.setEmpSex(emp.getEmpSex());
        empVO.setEmpSalary(emp.getEmpSalary());
    }
}
